<?php

namespace app\controller;

use app\service\ProductionManageService;
use support\Request;
use support\Response;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class ProductionManageController
{

    /**
     * @var ProductionManageService
     */
    private $productionManageService;

    public function __construct()
    {
        $this->productionManageService = ProductionManageService::getInstance();
    }

    /**
     * 今日统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function todayCount(Request $request): Response
    {
        $param = $request->post();
        $res = $this->productionManageService->todayCount($param);
        return json(success_response('success', $res));
    }

    /**
     * 获取当前月计划代办任务进度
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPlanTodoTaskProgress(Request $request): Response
    {
        $param = $request->post();
        $res = $this->productionManageService->getPlanTodoTaskProgress($param['project_id'], $param['episode_id']);
        return json(success_response('', $res));
    }


    /**
     * 总进度详情任务统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function totalTaskCount(Request $request): Response
    {
        $param = $request->post();
        $res = $this->productionManageService->totalTaskCount($param['param']['filter']);
        return json(success_response('', $res));
    }


    /**
     * 获取当前月计划代办任务进度
     * @param Request $request
     * @return Response
     * @throws DbException
     */
    public function episodeTaskCount(Request $request): Response
    {
        $param = $request->post();

        $projectId = $param['project_id'] ?? 0;
        $stepCategoryIds = param_to_array($param['step_category_ids'] ?? '', ',', 'intval');
        $episodeId = $param['episode_id'] ?? 0;

        $res = $this->productionManageService->episodeTaskCount($request, $projectId, $stepCategoryIds, $episodeId);
        return json(success_response('', $res));
    }

    /**
     * 今日统计
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function progressCount(Request $request): Response
    {
        $param = $request->post();
        $res = $this->productionManageService->progressCount($param);
        return json(success_response('', $res));
    }

    /**
     * 审核反馈统计
     * @param Request $request
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function reviewFeedbackCount(Request $request): Response
    {
        $param = $request->post();

        $projectId = $param['project_id'] ?? 0;
        $reviewFeedbackId = $param['review_feedback_id'] ?? 0;
        $episodeCode = $param['episode_code'] ?? '';

        $res = $this->productionManageService->reviewFeedbackCount($request, $reviewFeedbackId, $projectId, $episodeCode);
        return json(success_response('', $res));
    }
}
